Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pluggable Regex Engines #112

Merged
merged 8 commits into from
Jan 1, 2025
Merged

Conversation

JemDay
Copy link
Contributor

@JemDay JemDay commented Dec 4, 2024

By default the underlying JSONSchema validator uses the standard Go regular expression engine; this engine supports the RE2 syntax which may result in failures during validation of OAS specifications that use non-RE2 compliant patterns.

The change introduces a configuration construct for each validator that may be passed at time of creation to affect the way it operates.

Initially this construct allows for the regular expression engine to be overriden by a user-supplied variant.

Closes #111

Copy link

codecov bot commented Dec 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.74%. Comparing base (c7a822d) to head (8b9e667).
Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #112   +/-   ##
=======================================
  Coverage   99.74%   99.74%           
=======================================
  Files          28       29    +1     
  Lines        3126     3156   +30     
=======================================
+ Hits         3118     3148   +30     
  Misses          8        8           
Flag Coverage Δ
unittests 99.74% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

By default the underlying JSONSchema validator uses the standard Go
regular expression engine; this engine supports the RE2 syntax which
may result in failures during validation of OAS specifcations that
use non-RE2 compliant patterns.

The change introduces a configuration construct for each validator
that may be passed at time of creation to affect the way it operates.

Initially this construct allows for the regular expression engine to
be overriden by a user-supplied variant.

Closes pb33f#111
@JemDay JemDay changed the title Config PoC: DO NOT MERGE Add support for pluggable Regex Engines Dec 10, 2024
.gitignore Outdated Show resolved Hide resolved
parameters/parameters.go Outdated Show resolved Hide resolved
requests/request_body.go Outdated Show resolved Hide resolved
requests/request_body.go Outdated Show resolved Hide resolved
- Removed .gitignore
- Fixed import ordering
- Adopted 'Options pattern' for configuration.
@JemDay
Copy link
Contributor Author

JemDay commented Dec 11, 2024

@ccoVeille - Thanks for taking the time to poke through this ... the Options pattern will provide a much more fluent style and hopefully can be expanded in the future.

responses/response_body.go Outdated Show resolved Hide resolved
responses/validate_response.go Outdated Show resolved Hide resolved
schema_validation/validate_schema.go Outdated Show resolved Hide resolved
validator.go Outdated Show resolved Hide resolved
validator_test.go Outdated Show resolved Hide resolved
validator.go Outdated Show resolved Hide resolved
parameters/parameters.go Outdated Show resolved Hide resolved
schema_validation/validate_document.go Outdated Show resolved Hide resolved
schema_validation/validate_schema.go Outdated Show resolved Hide resolved
validator_test.go Outdated Show resolved Hide resolved
validator_test.go Outdated Show resolved Hide resolved
validator_test.go Outdated Show resolved Hide resolved
@ccoVeille
Copy link

@ccoVeille - Thanks for taking the time to poke through this ... the Options pattern will provide a much more fluent style and hopefully can be expanded in the future.

You are welcome.

Do you mind if I quote you and this PR on Dev.to article I plan to write in the next days?

Here is my profile https://dev.to/ccoveille

@JemDay
Copy link
Contributor Author

JemDay commented Dec 11, 2024

@ccoVeille - Thanks for taking the time to poke through this ... the Options pattern will provide a much more fluent style and hopefully can be expanded in the future.

You are welcome.

Do you mind if I quote you and this PR on Dev.to article I plan to write in the next days?

Here is my profile https://dev.to/ccoveille

Sure (he said guardedly!) would appreciate cleaning this up first.

- Updated documentation.
- Fully adopted 'Options pattern'.
- Addressed linting issues.
- Stricter Unit Test assertions.
requests/request_body.go Outdated Show resolved Hide resolved
requests/request_body.go Outdated Show resolved Hide resolved
requests/validate_request.go Outdated Show resolved Hide resolved
responses/response_body.go Outdated Show resolved Hide resolved
schema_validation/validate_schema.go Outdated Show resolved Hide resolved
@JemDay
Copy link
Contributor Author

JemDay commented Dec 12, 2024

@ccoVeille - Thanks again for the comments

If I get time tomorrow I'll see if it's possible to lift the config out completely and make it truly common without introducing circular dependencies and unintended publicly visible constructs.

- Established new 'config' package
- Establish a (hopefully) extensible mechanism and pattern to support config  options.
- Less duplicative code.
- Established a (hopefully) extenible model for any future config needs.
config/config.go Outdated Show resolved Hide resolved
- Updated options structure name.
Comment on lines 60 to 74
func NewSchemaValidatorWithLogger(logger *slog.Logger, opts ...config.Option) SchemaValidator {

options := config.NewValidationOptions(opts...)

return &schemaValidator{options: options, logger: logger, lock: sync.Mutex{}}

}

// NewSchemaValidator will create a new SchemaValidator instance, ready to accept schemas and payloads to validate.
func NewSchemaValidator() SchemaValidator {
func NewSchemaValidator(opts ...config.Option) SchemaValidator {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
return NewSchemaValidatorWithLogger(logger)
return NewSchemaValidatorWithLogger(logger, opts...)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewSchemaValidatorWithLogger was there before this PR, OK

But for me, you could consider to deprecate it and add a WithLogger Option

It doesn't have to be done in this PR, of course.

But I think it would be a huge improvement

But usually, the way to do that is to have a WithDefaultLogger(), that create the logger then it calls WithLogger(logger)

The WithDefaultLogger() is added in the options []Options slice as first argument.

Another way is to check is to do the opposite and appended on the options slice, but the WithDefaultLogger check if the logger is already defined and do nothing if present

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make sense ...

Thanks again for taking the time to poke through this, I guess the appetite for adopting the fluent style more broadly would rest with the owners.

v.(*validator).document = document
return v, nil
}

// NewValidatorFromV3Model will create a new Validator from an OpenAPI Model
func NewValidatorFromV3Model(m *v3.Document) Validator {
func NewValidatorFromV3Model(m *v3.Document, opts ...config.Option) Validator {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with FromV3Model, that would become a WithV3Model(m)

@JemDay
Copy link
Contributor Author

JemDay commented Dec 18, 2024

@daveshanley - Any reservations/thoughts on this ?

@daveshanley
Copy link
Member

@daveshanley - Any reservations/thoughts on this ?

I have not had a chance to read through it yet to be honest, I have a few PRs to review and I am stuck on another set of libopenapi problems at the moment.

I will get here as soon as I can.

@daveshanley
Copy link
Member

daveshanley commented Dec 18, 2024

build seems to be failing linting

@JemDay
Copy link
Contributor Author

JemDay commented Dec 18, 2024

build seems to be failing linting

Hmm ... will check ..

@emilien-puget
Copy link
Contributor

build seems to be failing linting

Hmm ... will check ..

Some files are not formatted using gofumpt, you can find the command in the makefile

@JemDay
Copy link
Contributor Author

JemDay commented Dec 19, 2024

Linting issues resolved ..

@daveshanley
Copy link
Member

Linting issues resolved ..

I am coming to the end of my latest feature run, I will switch back to PRs soon. Thank you for your patience.

Copy link
Member

@daveshanley daveshanley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you for your contribution and thank you @ccoVeille for all your input!

@daveshanley daveshanley merged commit 7859478 into pb33f:main Jan 1, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support alternate Pattern/RegEx engine
4 participants